package org.bbs.demo.controller;

import org.bbs.demo.common.BBSConstants;
import org.bbs.demo.common.Result;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.constraints.Min;
import javax.validation.constraints.NotNull;

@Validated
@RestController
@Transactional
public class ControllerTest {

    @GetMapping("/Hello")
    public String hello(){
        return "Hello World!";
    }

    @GetMapping("/tokenHello")
    public String tokenHello(){
        return "Hello World!";
    }

    @PostMapping("/test1")
    public Result test1(
            @NotNull(message = "输入不能为空")
            @Min(value = 0, message = "输入不能为负数")
            Integer num
    ) {

        if (num % 2 == 0){
            return Result.OK();
        }
        else {
            return Result.err(BBSConstants.CODE_ERR_BUSINESS, "不允许奇数");
        }
    }

    @PostMapping("/test2")
    public Result test2(@RequestHeader String Token){

        return Result.OK((Object) Token);
    }

}